package main

var(
	ddx = [4]int{0,0,-1,1}
	ddy = [4]int{1,-1,0,0}
)

func maxAreaOfIsland(grid [][]int) int{
	maxArea,newArea,area:= 0,0,0
	m,n := len(grid),len(grid[0])
	for i:=0;i<m;i++{
		for j:=0;j<n;j++{
			//遇到1进行深搜
			if grid[i][j]==1 {
				newArea,grid= dfsArea(i,j,area,grid)
				if newArea>maxArea{
					maxArea = newArea
				}
			}
		}
	}
	return maxArea
}

func dfsArea(x int, y int,area int, grid [][]int) (int,[][]int){
	if grid[x][y]==1{
		//搜索过的节点置为0，防止重复搜索
		area++
		grid[x][y]=0
		//搜索上下左右
		newx,newy := 0,0
		for i:=0;i<4;i++{
			newx = x+ddx[i]
			newy = y+ddy[i]
			if newx>=0&&newx<len(grid)&&newy>=0&&newy<len(grid[0])&&grid[newx][newy]==1{
				area,grid = dfsArea(newx,newy,area,grid)
			}
		}
	}
	return area,grid
}

func main(){
	test := [][]int{}
	test = append(test,[]int{1,1})
	print(maxAreaOfIsland(test))

}